package com.yonyou.gmmc.service.pointactivity.util;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.util.ByteArrayDataSource;

/**
 * 发送邮件的作业
 * 
 * @author 徐庆锋
 *
 */

public class MailJob {

	public static void execute(String content, String title, String sendEmail, String password, String smtpserver,
			List<String> acceptEmailList, List<Map<String, Object>> listOriginal) throws Exception {
		System.out.println("要发邮件了。。。" + new Date());
		final Properties mailProps = new Properties();
		mailProps.put("mail.smtp.host", smtpserver);
		mailProps.put("mail.smtp.auth", "true");
		mailProps.put("mail.sendEmail", sendEmail);
		mailProps.put("mail.password", password);
		mailProps.put("mail.smtp.port", "25");

		// 构建授权信息，用于进行SMTP进行身份验证
		Authenticator authenticator = new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				// 验证用户名、密码
				String sendEmail = mailProps.getProperty("mail.sendEmail");
				String password = mailProps.getProperty("mail.password");
				return new PasswordAuthentication(sendEmail, password);
			}
		};
		
		// 使用环境属性和授权信息，创建邮件会话
		Session mailSession = Session.getInstance(mailProps, authenticator);
		for (int i = 0; i < acceptEmailList.size(); i++) {
			// 创建邮件消息
			MimeMessage message = new MimeMessage(mailSession);
			// 设置发件人
			InternetAddress from = new InternetAddress(mailProps.getProperty("mail.sendEmail"));
			message.setFrom(from);
			// 设置收件人
			InternetAddress to = new InternetAddress(acceptEmailList.get(i));
			message.setRecipient(RecipientType.TO, to);
			// 设置邮件标题
			message.setSubject(title);
			// 设置邮件的内容体
			BodyPart part1 = new MimeBodyPart();
			BodyPart part2 = new MimeBodyPart();
			part1.setContent(content, "text/html;charset=UTF-8");

			/**
			 * 发送多个附件
			 */
			Multipart attachments = new MimeMultipart();
			for (Map<String, Object> map : listOriginal) {

				BodyPart attach1 = new MimeBodyPart();
				// 设置附件的内容
				/*
				 * 流对象也可用 InputStream input =(InputStream) map.get("input");
				 * DataSource dataSource_attach1 = new
				 * ByteArrayDataSource(input,"application/png");
				 */
				byte[] buffer = (byte[]) map.get("buffer");
				DataSource dataSource_attach1 = new ByteArrayDataSource(buffer, "application/png");
				DataHandler dataHandler_attach1 = new DataHandler(dataSource_attach1);
				attach1.setDataHandler(dataHandler_attach1);
				attach1.setFileName(MimeUtility.encodeText(map.get("uploadFileName").toString()));
				attachments.addBodyPart(attach1);
			}
			part2.setContent(attachments);

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(part1);
			multipart.addBodyPart(part2);
			message.setContent(multipart);
			Transport.send(message);// 发送
		}
	}
	
	public static void execute2(String content, String title, String sendEmail, String password, String smtpserver,
			List<String> acceptEmailList, List<Map<String, Object>> listOriginal) throws Exception {
		System.out.println("要发邮件了。。。" + new Date());
		final Properties mailProps = new Properties();
		mailProps.put("mail.smtp.host", smtpserver);
		mailProps.put("mail.smtp.auth", "true");
		mailProps.put("mail.sendEmail", sendEmail);
		mailProps.put("mail.password", password);
		mailProps.put("mail.smtp.port", "25");

		// 构建授权信息，用于进行SMTP进行身份验证
		Authenticator authenticator = new Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				// 验证用户名、密码
				String sendEmail = mailProps.getProperty("mail.sendEmail");
				String password = mailProps.getProperty("mail.password");
				return new PasswordAuthentication(sendEmail, password);
			}
		};
		// 使用环境属性和授权信息，创建邮件会话
		Session mailSession = Session.getInstance(mailProps, authenticator);
		for (int i = 0; i < acceptEmailList.size(); i++) {
			// 创建邮件消息
			MimeMessage message = new MimeMessage(mailSession);
			// 设置发件人
			InternetAddress from = new InternetAddress(mailProps.getProperty("mail.sendEmail"));
			message.setFrom(from);
			// 设置收件人
			InternetAddress to = new InternetAddress(acceptEmailList.get(i));
			message.setRecipient(RecipientType.TO, to);
			// 设置邮件标题
			message.setSubject(title);
			// 设置邮件的内容体
			BodyPart part1 = new MimeBodyPart();
			BodyPart part2 = new MimeBodyPart();
			part1.setContent(content, "text/html;charset=UTF-8");

			/**
			 * 发送多个附件
			 */
			Multipart attachments = new MimeMultipart();
			for (Map<String, Object> map : listOriginal) {

				BodyPart attach1 = new MimeBodyPart();
				// 设置附件的内容
				File uploadFile = (File) map.get("uploadFile");
				DataSource dataSource_attach1 = new FileDataSource(uploadFile);
				DataHandler dataHandler_attach1 = new DataHandler(dataSource_attach1);
				attach1.setDataHandler(dataHandler_attach1);
				attach1.setFileName(MimeUtility.encodeText(map.get("uploadFileName").toString()));
				attachments.addBodyPart(attach1);
			}
			part2.setContent(attachments);

			Multipart multipart = new MimeMultipart();
			multipart.addBodyPart(part1);
			multipart.addBodyPart(part2);
			message.setContent(multipart);
			Transport.send(message);// 发送
		}
	}
	

}
